﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Clotho.Novelview {
	public partial class PlotC : UserControl {
		private PlotC() {
			InitializeComponent();
		}
		public readonly Plot bgd;
		public readonly ViewFeature view;
		public readonly NovelviewControl novel_view;
		string view_name => novel_view.view_name;

		public bool Is_selected {
			get {
				return bgd.Selected;
			}
			set {
				if (value != bgd.Selected) {
					bgd.Selected = value;
				}
			}
		}
		public event EventHandler eClickSelected;
		public event EventHandler eBeginInput;
		public event EventHandler eMoveHotZoneMouseDown;
		protected override void OnPaint(PaintEventArgs e) {
			base.OnPaint(e);
			if (Is_selected) {
				Graphics g = e.Graphics;
				g.DrawRectangle(novel_view.cBrush.Selected_plot_outline, 1,1,Width-2,Height-2);
			}
		}

		public PlotC(Plot p, NovelviewControl novel_view) {
			InitializeComponent();
			DoubleBuffered = true;
			bgd = p;
			TitleTB.Text = bgd.Name;
			DescTB.Text = bgd.Description;
			//TitleTB.DataBindings.Add("Text", bgd, "Name", false, DataSourceUpdateMode.OnValidation);
			//DescTB.DataBindings.Add("Text", bgd, "Description", false, DataSourceUpdateMode.OnValidation);
			this.novel_view = novel_view;
			view = bgd.getFeature<ViewFeature>(view_name);
			mainKW.plot = bgd;
			mainKW.Parent = this;
			bgd.eNameChanged += Bgd_eNameChanged;
			bgd.eDescriptionChanged += Bgd_eDescriptionChanged;
			bgd.eSelectChange += (sender,e)=> 
			Refresh();

			DescTB.GotFocus += _GotFocus;
			TitleTB.GotFocus += _GotFocus;
			DescTB.GotFocus += _BeginInput;
			TitleTB.GotFocus += _BeginInput;
			mainKW.MouseDown += OnMoveHotZoneMouseDown;
			void _GotFocus(object sender, EventArgs e) {
				eClickSelected?.Invoke(this, EventArgs.Empty);
			}
			void _BeginInput(object sender, EventArgs e) {
				eBeginInput?.Invoke(this, EventArgs.Empty);
			}
			TitleTB.Validating += TitleTB_Validating;
			DescTB.Validating += DescTB_Validating;
		}

		protected virtual void OnMoveHotZoneMouseDown(object sender, EventArgs e) {
			eMoveHotZoneMouseDown?.Invoke(this, e);
		}

		private void Bgd_eDescriptionChanged(object obj, EventArgs e) {
			DescTB.Text = bgd.Description;
		}

		private void Bgd_eNameChanged(object obj,EventArgs e) {
			TitleTB.Text = bgd.Name;
		}

		private void TitleTB_Validating(object sender, CancelEventArgs e) {
			if(bgd.Name != TitleTB.Text) {
				bgd.Nov.addCmdKnot();
				try {
					bgd.Name = TitleTB.Text;
				} catch (InvalidCmd_Exception ex) {
					novel_view.showError(this.Location, ex.Message);
					bgd.Nov.undo();
				}
			}
		}


		private void DescTB_Validating(object sender, CancelEventArgs e) {
			if (bgd.Description != DescTB.Text) {
				bgd.Nov.addCmdKnot();
				bgd.Description = DescTB.Text;
			}
		}



		/// <summary>
		/// 将情节按照给定的内存数据移动到指定位置并调整大小，需要在放入控件后执行。
		/// </summary>
		public void resetLocation(Point pix_for_origin) {
			var location = view.Location;
			location.Offset(pix_for_origin);
			Location = location;
		}
		public void resetSize(Point pix_for_origin) {
			Size = view.Size;
			if (Is_selected) {
				novel_view.resetResizer();
			}
		}


		public override string ToString() {
			return $"plot:{TitleTB.Text}";
		}

		private void DescTB_TextChanged(object sender, EventArgs e) {

		}
	}
}
